<!--stmop  websocket -->
var stompClient;
// let bookCard;
// let userCard;
let userCardMsg,bookCardMsg;
$(function($) {
    var socket = new SockJS('/gs-guide-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        /*传感器一数据*/
        //烟雾传感器1状态
        stompClient.subscribe("/topic/sensor", function (response) {
            $("#sensor1").text(response.body);
        });
        stompClient.subscribe("/topic/time2", function (response) {
            $("#time2").text(response.body);
        });
        //烟雾传感器2状态
        stompClient.subscribe("/topic/sensor", function (response) {
            $("#sensor2").text(response.body);
        });
        stompClient.subscribe("/topic/time2", function (response) {
            $("#time3").text(response.body);
        });
        //开馆闭馆状态
        stompClient.subscribe("/topic/state", function (response) {
            $("#state").text(response.body);
        });
        stompClient.subscribe("/topic/time1", function (response) {
            $("#time1").text(response.body);
        });
        //红外检测非法闯入信息
        stompClient.subscribe("/topic/illegal", function (response) {
            $("#illegal").text(response.body);
        });
        stompClient.subscribe("/topic/time4", function (response) {
            $("#time4").text(response.body);
        });
        //接收到书卡号
        stompClient.subscribe("/topic/bookCard", function (response) {
            $("#bookCard").text(response.body);
        });
        //接收到用户卡
        stompClient.subscribe("/topic/userCard", function (response) {
            $("#userCard").text(response.body);
        });

        //录入失败
        stompClient.subscribe("/topic/fail", function (response) {
            alert(response.body);
        });
        //
        stompClient.subscribe("/topic/userCardMsg", function (response) {
            userCardMsg = response.body;
        });
        stompClient.subscribe("/topic/bookCardMsg", function (response) {
            bookCardMsg = response.body;
        });
        stompClient.subscribe("/topic/str", function (response) {
            alert(response.body);
        });
        stompClient.subscribe("/topic/str1", function (response) {
            alert(response.body);
        });
        //------------------------
        //借书还书状态
        stompClient.subscribe("/topic/bookCardMsg", function (response) {
            $("#bookMsg").text(response.body);
        });
        stompClient.subscribe("/topic/userCardMsg", function (response) {
            $("#peopleMsg").text(response.body);
        });
        stompClient.subscribe("/topic/bookCardMsg", function (response) {
            $("#bookMsg1").text(response.body);
        });
        stompClient.subscribe("/topic/userCardMsg", function (response) {
            $("#peopleMsg1").text(response.body);
        });

    });
});
//向硬件的同学发送一条消息
function sendMessage(message) {
    let param=new URLSearchParams();
    param.append("message",message);
    axios.post("sendMessage",param);
}
//开馆闭馆
function openLibrary(){
    sendMessage("Hwcgg0202onT");
    alert("开馆成功")
    //$("#state").text("开馆");
};
function closeLibrary(){
    sendMessage("Hwcgg0202ofT");
    alert("闭馆成功")
    //$("#state").text("闭馆");
};
//图书借阅/归还
function jieshu(){
    sendMessage("Hwcjs0304bookT");
}
function borrowBook(){
    //这里要更新时间以及图书状态
    let book1=$("#book1").val();
    let people1=$("#people1").val();
    let param = new URLSearchParams();
    param.append("userCard",book1);
    param.append("bookCard",people1);
    axios.post("insertBook",param)
        .then(function(){
            alert("借阅图书")
        })
};
function guihuan(){
    sendMessage("Hwchs0304bookT");
}
function returnBook(){
    let book2=$("#book2").val();
    let people2=$("#people2").val();
    //这里要更新时间以及图书状态
    let param = new URLSearchParams();
    param.append("userCard",people2);
    param.append("bookCard",book2);
    axios.post("updateBook",param)
        .then(function(){
            alert("归还图书")
        })
};
//--------------图书信息录入模块--------------------
function getbookKA(){
    sendMessage("Hwcls0302onT");
}
function inputBook(){

    let bookNo=$("#number").val();
    let bookName=$("#bookName").val();
    let author=$("#author").val();
    let bookNum=$("#bookNum").val();
    let bookCard1=$("#bookCard1").val();
    if(bookNo===""||bookName===""||author===""||bookCard1===""){
        alert("选项不能为空！！")
    }else {
        let param=new URLSearchParams();
        param.append("bookNo",bookNo);
        param.append("bookName",bookName);
        param.append("author",author);
        param.append("bookNum",bookNum);
        param.append("bookCard",bookCard1);
        axios.post("insertBookInfo",param)
            .then(function () {
                alert("录入成功");
            })
    }
}
//--------------用户信息录入模块--------------------
function getuserCard(){
    sendMessage("Hwclr0302onT")//录入
}
function inputUser(){
    let name = $("#name").val();
    let sex = $("input[name='sex']:checked").val();
    let age=$("#select1 option:checked").text();
    let grade=$("#select2 option:checked").text();
    let userCard1 = $("#userCard1").val();
    if(name===""||sex===""||age===""||grade===""||userCard1===""){
        alert("选项不能为空！！")
    }else {
        let param=new URLSearchParams();
        param.append("userName",name);
        param.append("sex",sex);
        param.append("age",age);
        param.append("grade",grade);
        param.append("userCard",userCard1);
        axios.post("insertUser",param)
            .then(function(){
                alert("录入成功");
            })
    }
}
//--------------图书借阅信息查询模块--------------------
function queryBookByBookNo(){
    let bookNo=$("#bookNo").val();
    let param = new URLSearchParams();
        param.append("bookNo",bookNo);
        axios.post("queryBookByBookNo",param)
            .then(function() {
                alert("查询成功");
            })
}
function queryBookByBorrower(){
    let borrower=$("#borrower").val();
    let param = new URLSearchParams();
    param.append("borrower",borrower);
    axios.post("queryBookByBorrower",param)
        .then(function(){
            alert(borrower);
        })
}
function queryBookByborrowTime(){
    let borrowTime=$("#borrowTime").val();
    let param = new URLSearchParams();
    param.append("borrowTime",borrowTime);
    axios.post("queryBookByborrowTime",param)
        .then(function() {
            alert("查询成功");
        })
}
/*----------------------按书号图形统计-------------------------*/
var myChart = echarts.init(document.getElementById('line1'));
setInterval(function () {
$.ajax({
    contentType: "application/json",
    type: "POST",
    url: "queryAllbook",
    dataType: "json",
    success: function (data) {
        let x = [];
        let y = [];

        for (let i = 0; i < data.length; i++) {
            x[i] = data[i].bookNo;
            y[i] = data[i].bookNum;
        }
        myChart.setOption({
            title: {
                text: '按书号统计',
            },
            tooltip: {},
            label: {
                show: true,
                position:'top',
            },
            legend: {},
            xAxis: {
                data: x
            },
            yAxis: {},
            series: [
                {
                    colorBy:'data',
                    name: '数量',
                    type: 'bar',
                    data: y

                }
            ]
        });

    },
    error: function () {
    }
 })
},1000)
/*----------------------按借阅人统计-------------------------*/
let myChart1 = echarts.init(document.getElementById('line2'));
setInterval(function () {
    $.ajax({
        contentType: "application/json",
        type: "POST",
        url: "queryCount",
        dataType: "json",
        success: function (data) {
            let x = [];
            let y = [];

            for (let i = 0; i < data.length; i++) {
                x[i] = data[i].borrower;
                y[i] = data[i].bookNum;
            }
            myChart1.setOption({
                title: {
                    text: '按借阅人统计',
                },
                tooltip: {},
                label: {
                    show: true,
                    position:'top',
                },
                legend: {},
                xAxis: {
                    data: x
                },
                yAxis: {},
                series: [
                    {
                        colorBy:'data',
                        name: '数量',
                        type: 'bar',
                        data: y

                    }
                ]
            });

        },
        error: function () {
        }
    })
}, 1000)
/*----------------------按借阅人图形统计----------------------*/